package com.his.struts.dao.function;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.his.struts.dao.hospitalInfo.HospitalInfoImp;
import com.his.struts.form.FunctionForm;
import com.his.struts.util.DbUtil;

public class FunctionImp implements FunctionDao{

	Logger log = Logger.getLogger(HospitalInfoImp.class);
	DbUtil db = new DbUtil();
	
	/**
	 * 增加用户功能
	 * @param function 功能
	 */
	public void addUserFunction(FunctionForm function) {
		ArrayList list = new ArrayList();
		String sql="insert into userFunction(fname,furl) values(?,?)";
		list.add(function.getFname());
		list.add(function.getFurl());
		db.executeUpdate(sql, list);
		db.closeAll();
	}
	
	/**
	 * 增加医生功能
	 * @param function 功能
	 */
	public void addDoctorFunction(FunctionForm function) {
		ArrayList list = new ArrayList();
		String sql="insert into doctorFunction(fname,furl) values(?,?)";
		list.add(function.getFname());
		list.add(function.getFurl());
		db.executeUpdate(sql, list);
		db.closeAll();
	}
	
	/**
	 * 删除用户功能
	 * @param fid 功能id
	 */
	public void delUserFunction(int fid) {
		ArrayList list = new ArrayList();
		String sql="delete from userFunction where fid=?";
		list.add(fid);
		db.executeUpdate(sql, list);
		db.closeAll();
	}
	
	/**
	 * 删除医生功能
	 * @param fid 功能id
	 */
	public void delDoctorFunction(int fid) {
		ArrayList list = new ArrayList();
		String sql="delete from doctorFunction where fid=?";
		list.add(fid);
		db.executeUpdate(sql, list);
		db.closeAll();
	}
	
	/**
	 * 修改用户功能
	 * @param function 功能
	 */
	public void updateUserFunction(FunctionForm function) {
		ArrayList list = new ArrayList();
		String sql="update userFunction set fname=?,furl=? where fid=?";
		list.add(function.getFname());
		list.add(function.getFurl());
		list.add(function.getFid());
		db.executeUpdate(sql, list);
		db.closeAll();
	}
	
	/**
	 * 修改医生功能
	 * @param function 功能
	 */
	public void updateDoctorFunction(FunctionForm function) {
		ArrayList list = new ArrayList();
		String sql="update doctorFunction set fname=?,furl=? where fid=?";
		list.add(function.getFname());
		list.add(function.getFurl());
		list.add(function.getFid());
		db.executeUpdate(sql, list);
		db.closeAll();
	}
	
	/**
	 * 读取用户所拥有的功能
	 * @return 用户所拥有的功能
	 */
	public List<FunctionForm> getUserFunction() {
		List<FunctionForm> list = new ArrayList();
		String sql="select * from userFunction";
		ResultSet rs = db.executeQuery(sql, null);
		try {
			while (rs.next()){
				FunctionForm function = new FunctionForm();
				function.setFid(rs.getInt("fid"));
				function.setFname(rs.getString("fname"));
				function.setFurl(rs.getString("furl"));
				list.add(function);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		}finally{
			db.closeAll();
		}
		
		return list;
	}
	
	/**
	 * 读取医生所拥有的功能
	 * @return 医生所拥有的功能
	 */
	public List<FunctionForm> getDoctorFunction() {
		List<FunctionForm> list = new ArrayList();
		String sql="select * from doctorFunction";
		ResultSet rs = db.executeQuery(sql, null);
		try {
			while (rs.next()){
				FunctionForm function = new FunctionForm();
				function.setFid(rs.getInt("fid"));
				function.setFname(rs.getString("fname"));
				function.setFurl(rs.getString("furl"));
				list.add(function);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		}finally{
			db.closeAll();
		}
		return list;
	}
	
}
